﻿(function() {
  var Grid, GridColumn, GridData, GridDefault, GridTypes,
    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
    __hasProp = {}.hasOwnProperty,
    __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };

  Grid = (function() {

    function Grid(table, options) {
      var response;
      this.table = table;
      this.options = options;
      this.getCheckedRows = __bind(this.getCheckedRows, this);

      this.invert = __bind(this.invert, this);

      this.checkUncheckData = __bind(this.checkUncheckData, this);

      this.unhcheckData = __bind(this.unhcheckData, this);

      this.checkData = __bind(this.checkData, this);

      this.checkUncheckAll = __bind(this.checkUncheckAll, this);

      this.uncheckAll = __bind(this.uncheckAll, this);

      this.checkAll = __bind(this.checkAll, this);

      this.checkUncheckPage = __bind(this.checkUncheckPage, this);

      this.uncheckPage = __bind(this.uncheckPage, this);

      this.checkPage = __bind(this.checkPage, this);

      this.makeOptions = __bind(this.makeOptions, this);

      this.makeButtons = __bind(this.makeButtons, this);

      this.renderCell = __bind(this.renderCell, this);

      this.renderLine = __bind(this.renderLine, this);

      this.remakeBody = __bind(this.remakeBody, this);

      this.id = this.table.prop("id");
      this.table.addClass("rhd-grid table");
      this.container = $("<div>", {
        "class": "rhd-grid-container"
      });
      this.container.insertAfter(this.table);
      this.container.wrapInner(this.table);
      this.container.hide();
      if (this.options.responsive) {
        response = $("<div>", {
          "class": "table-responsive"
        });
        response.appendTo(this.container);
        response.wrapInner(this.table);
      }
      this.init();
    }

    Grid.prototype.init = function() {
      this.initColumns();
      this.make();
      this.data = new GridData(this, this.options.data);
      this.table.data("rhd.grid", this);
      return this.table.triggerHandler("init.rhd.grid");
    };

    Grid.prototype.initColumns = function() {
      var col, i, _i, _len, _ref, _results;
      this.columns = [];
      if (this.options.multiSelect) {
        this.columns.push(new GridColumn({
          active: true,
          multiSelect: true,
          header: "multiselect",
          renderer: "multiselect",
          order: 0
        }));
      }
      i = 0;
      _ref = this.options.columns;
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        col = _ref[_i];
        col.order = ++i;
        _results.push(this.columns.push(new GridColumn(col)));
      }
      return _results;
    };

    Grid.prototype.refresh = function() {
      return this.data.loadData();
    };

    Grid.prototype.loadData = function(url, postData, method) {
      return this.data.loadData(url, postData, method);
    };

    Grid.prototype.ready = function() {
      this.hideDialog();
      this.remake();
      return this.table.triggerHandler("ready.rhd.grid");
    };

    Grid.prototype.make = function() {
      this.makeDialog();
      this.makeHeader();
      return this.makeBody();
    };

    Grid.prototype.remake = function() {
      this.remakeHeader();
      return this.remakeContent();
    };

    Grid.prototype.remakeContent = function() {
      this.remakeBody();
      this.remakeComponents();
      return this.table.triggerHandler("remake.rhd.grid");
    };

    Grid.prototype.makeDialog = function() {
      this.dialog = $("<div>", {
        "class": "rhd-grid-dialog alert alert-info"
      });
      this.dialog.insertBefore(this.container);
      return this.dialog.hide();
    };

    Grid.prototype.showDialog = function(msg) {
      this.container.hide();
      this.dialog.html(msg);
      return this.dialog.show();
    };

    Grid.prototype.hideDialog = function() {
      this.dialog.hide();
      return this.container.show();
    };

    Grid.prototype.makeHeader = function() {
      this.thead = $("<thead>");
      return this.thead.appendTo(this.table);
    };

    Grid.prototype.remakeHeader = function() {
      var col, header, th, _i, _len, _ref, _results;
      this.thead.html("");
      header = $("<tr>");
      header.appendTo(this.thead);
      _ref = this.getActiveColumns();
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        col = _ref[_i];
        th = $("<th>");
        th.appendTo(header);
        _results.push(this.makeHeaderCell(th, col));
      }
      return _results;
    };

    Grid.prototype.makeHeaderCell = function(th, col) {
      var options, renderer;
      renderer = $.rhd.grid.header._default;
      options = col.options.headerData || {};
      if ($.isFunction(col.options.header)) {
        renderer = col.options.header;
      } else if (col.options.header != null) {
        renderer = $.rhd.grid.header[col.options.header];
        if (!(renderer != null)) {
          renderer = eval(col.options.header);
        }
      }
      return renderer.call(this, th, col, options);
    };

    Grid.prototype.makeBody = function() {
      this.tbody = $("<tbody>");
      this.tbody.appendTo(this.table);
      return this;
    };

    Grid.prototype.remakeBody = function() {
      var row, _i, _len, _ref, _results;
      this.tbody.html("");
      _ref = this.pageRows();
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        row = _ref[_i];
        _results.push(this.renderLine(row));
      }
      return _results;
    };

    Grid.prototype.remakeComponents = function() {
      this.table.triggerHandler("summary.nav.rhd.grid", this.makeSummary());
      this.table.triggerHandler("buttons.nav.rhd.grid", this.makeButtons());
      return this.table.triggerHandler("options.nav.rhd.grid", this.makeOptions());
    };

    Grid.prototype.renderLine = function(row) {
      var col, tr, _i, _len, _ref, _results;
      tr = $("<tr>");
      tr.appendTo(this.tbody);
      _ref = this.getActiveColumns();
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        col = _ref[_i];
        _results.push(this.renderCell(tr, row, col));
      }
      return _results;
    };

    Grid.prototype.renderCell = function(tr, row, col) {
      var options, renderer, td, value;
      td = $("<td>");
      td.appendTo(tr);
      renderer = $.rhd.grid.renderer._default;
      options = col.options.rendererData || {};
      if ($.isFunction(col.options.renderer)) {
        renderer = col.options.renderer;
      } else if (col.options.renderer != null) {
        renderer = $.rhd.grid.renderer[col.options.renderer];
        if (!(renderer != null)) {
          renderer = eval(col.options.renderer);
        }
      }
      value = col.getFormattedValue(row);
      renderer.call(this, td, value, col, row, options);
      return td;
    };

    Grid.prototype.makeIconSort = function(column) {
      var icon, span;
      if (!(column.sort != null) || !(column.sort.type != null) || column.sort.type === GridTypes.sortType.none) {
        return "";
      }
      span = $("<span>");
      icon = column.sort.type === GridTypes.sortType.asc ? "glyphicon glyphicon-chevron-up" : "glyphicon glyphicon-chevron-down";
      span.append($("<i>", {
        "class": icon
      }));
      return span.append(column.sort.order + 1);
    };

    Grid.prototype.toggleSort = function(column, group) {
      var type;
      type = column.sort.type;
      switch (type) {
        case GridTypes.sortType.asc:
          type = GridTypes.sortType.desc;
          break;
        case GridTypes.sortType.desc:
          type = GridTypes.sortType.asc;
          break;
        default:
          type = GridTypes.sortType.asc;
      }
      return this.sortAndGroup(column, type, group);
    };

    Grid.prototype.sortAndGroup = function(column, type, group) {
      var col, lastOrder, order, _i, _len, _ref;
      if (type !== GridTypes.sortType.asc && type !== GridTypes.sortType.desc) {
        type = GridTypes.sortType.none;
      }
      order = 0;
      if (group) {
        if (!(column.sort.order != null)) {
          lastOrder = -1;
          _ref = this.getColumns();
          for (_i = 0, _len = _ref.length; _i < _len; _i++) {
            col = _ref[_i];
            if ((col.sort != null) && lastOrder < col.sort.order) {
              lastOrder = col.sort.order;
            }
          }
          order = lastOrder + 1;
        } else {
          order = column.sort.order;
        }
      } else {
        this.removeSort();
      }
      column.sort.type = type;
      column.sort.order = order;
      this.sort();
      return this.remake();
    };

    Grid.prototype.removeSort = function() {
      var col, _i, _len, _ref, _results;
      _ref = this.getColumns();
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        col = _ref[_i];
        if (!(col.sort != null)) {
          continue;
        }
        col.sort.type = GridTypes.sortType.none;
        _results.push(col.sort.order = null);
      }
      return _results;
    };

    Grid.prototype.sort = function() {
      return this.sortData(this.data.rows);
    };

    Grid.prototype.sortData = function(data) {
      var col, sorted, _i, _len, _ref,
        _this = this;
      sorted = [];
      _ref = this.getColumns();
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        col = _ref[_i];
        if ((col.sort != null) && col.sort.type && col.sort.type !== GridTypes.sortType.none) {
          sorted.push(col);
        }
      }
      if (sorted.length > 0) {
        sorted.sort(function(a, b) {
          if (a.sort.order > b.sort.order) {
            return 1;
          } else {
            return -1;
          }
        });
        return data.sort(function(a, b) {
          return _this.sortRow(a, b, sorted);
        });
      }
    };

    Grid.prototype.sortRow = function(a, b, columns) {
      var col, result, _i, _len;
      result = -2;
      for (_i = 0, _len = columns.length; _i < _len; _i++) {
        col = columns[_i];
        result = this.sortCol(a, b, col);
        if (result !== 0) {
          return result;
        }
      }
      return result;
    };

    Grid.prototype.sortCol = function(a, b, col) {
      var aValue, bValue, sorter;
      aValue = col.getModelSortValue(a);
      bValue = col.getModelSortValue(b);
      sorter = $.rhd.grid.sorter[col.sort.dataType] || $.rhd.grid.sorter.string;
      if ($.isFunction(col.options.sort.sorter)) {
        sorter = col.options.sort.sorter;
      } else if (col.options.sort.sorter != null) {
        sorter = $.rhd.grid.sorter[col.options.sort.sorter];
        if (!(sorter != null)) {
          sorter = eval(col.options.sort.sorter);
        }
      }
      if (col.sort.type === GridTypes.sortType.asc) {
        return sorter.call(this, aValue, bValue);
      } else {
        return sorter.call(this, bValue, aValue);
      }
    };

    Grid.prototype.pageRows = function() {
      var index, _ref;
      if (!((_ref = this.data) != null ? _ref.rows : void 0)) {
        return [];
      }
      if (!(this.options.paging != null)) {
        return this.data.rows;
      }
      if (!(this.options.paging.currentPage != null)) {
        this.options.paging.currentPage = 1;
      }
      index = (this.options.paging.currentPage - 1) * this.options.paging.size;
      return this.data.rows.slice(index, index + this.options.paging.size);
    };

    Grid.prototype.getMultiSelect = function() {
      var col, multiselect, _i, _len, _ref;
      multiselect = null;
      _ref = this.getColumns();
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        col = _ref[_i];
        if (col.multiSelect) {
          multiselect = col;
        }
      }
      return multiselect;
    };

    Grid.prototype.getColumn = function(name) {
      var col, column, _i, _len, _ref;
      column = null;
      _ref = this.columns;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        col = _ref[_i];
        if ((col.name != null) && col.name === name) {
          column = col;
        }
      }
      return column;
    };

    Grid.prototype.getColumns = function() {
      return this.columns;
    };

    Grid.prototype.getActiveColumns = function() {
      var col, columns, _i, _len, _ref;
      columns = [];
      _ref = this.getSortedColumns();
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        col = _ref[_i];
        if (col.active) {
          columns.push(col);
        }
      }
      return columns;
    };

    Grid.prototype.getSortedColumns = function() {
      var col, columns, _i, _len, _ref;
      columns = [];
      _ref = this.columns;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        col = _ref[_i];
        columns.push(col);
      }
      columns.sort(function(a, b) {
        if (a.order > b.order) {
          return 1;
        } else {
          return -1;
        }
      });
      return columns;
    };

    Grid.prototype.makeSummary = function() {
      var first, last, total;
      total = this.data.rows.length;
      if (total > 0) {
        first = this.options.paging.currentPage === 1 ? 1 : (this.options.paging.size * (this.options.paging.currentPage - 1)) + 1;
        last = first + this.options.paging.size > total ? total : (first + this.options.paging.size) - 1;
      } else {
        first = 0;
        last = 0;
      }
      return $.rhd.grid.locale.paging.summary.format(first, last, total);
    };

    Grid.prototype.makeButtons = function() {
      var group, next, prev,
        _this = this;
      group = $("<span>", {
        "class": "btn-group"
      });
      prev = $("<a>", {
        "class": "btn btn-sm btn-default",
        title: $.rhd.grid.locale.paging.prev
      });
      if (this.getTotalPages() === 0 || this.options.paging.currentPage === 1) {
        prev.addClass("disabled");
      }
      prev.append($("<i>", {
        "class": "glyphicon glyphicon-chevron-left"
      }));
      prev.appendTo(group);
      prev.click(function() {
        return _this.prevPage();
      });
      next = $("<a>", {
        "class": "btn btn-sm btn-default",
        title: $.rhd.grid.locale.paging.next
      });
      if (this.getTotalPages() === 0 || this.options.paging.currentPage === this.getTotalPages()) {
        next.addClass("disabled");
      }
      next.append($("<i>", {
        "class": "glyphicon glyphicon-chevron-right"
      }));
      next.appendTo(group);
      next.click(function() {
        return _this.nextPage();
      });
      return group;
    };

    Grid.prototype.makeOptions = function() {
      var a, btnOptions, btnOptionsMenu, btnRefresh, group, li, size, _i, _len, _ref,
        _this = this;
      group = $("<div>", {
        "class": "btn-group"
      });
      btnRefresh = $("<a>", {
        "class": "btn btn-sm btn-default"
      });
      btnRefresh.append($("<i>", {
        "class": "glyphicon glyphicon-repeat"
      }));
      btnRefresh.appendTo(group);
      btnRefresh.click(function() {
        return _this.refresh();
      });
      btnOptions = $("<a>", {
        "class": "btn btn-sm btn-default dropdown-toggle",
        "data-toggle": "dropdown"
      });
      btnOptions.append($("<span>", {
        "class": "caret"
      }));
      btnOptions.appendTo(group);
      btnOptionsMenu = $("<ul>", {
        "class": "dropdown-menu"
      });
      btnOptionsMenu.appendTo(group);
      btnOptionsMenu.append($("<li/>", {
        "class": "dropdown-header"
      }).append($.rhd.grid.locale.paging.paging));
      btnOptionsMenu.append($("<li/>").append($("<a/>", {
        href: "#"
      }).append($.rhd.grid.locale.paging.first).click(function() {
        _this.firstPage();
        return false;
      })));
      btnOptionsMenu.append($("<li/>").append($("<a/>", {
        href: "#"
      }).append($.rhd.grid.locale.paging.last).click(function() {
        _this.lastPage();
        return false;
      })));
      btnOptionsMenu.append($("<li>", {
        "class": "dropdown-header"
      }).append($.rhd.grid.locale.paging.pageSize));
      _ref = this.options.paging.options;
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        size = _ref[_i];
        li = $("<li>");
        li.appendTo(btnOptionsMenu);
        a = $("<a>", {
          href: "#"
        });
        a.append(size);
        a.appendTo(li);
        a.click(function(event) {
          _this.setPageSize($(event.target).html());
          return false;
        });
      }
      this.table.triggerHandler("item.options.nav.rhd.grid", btnOptionsMenu);
      return group;
    };

    Grid.prototype.firstPage = function() {
      this.options.paging.currentPage = 1;
      return this.remakeContent();
    };

    Grid.prototype.prevPage = function() {
      if (this.options.paging.currentPage > 1) {
        this.options.paging.currentPage--;
      }
      return this.remakeContent();
    };

    Grid.prototype.nextPage = function() {
      if (this.options.paging.currentPage < this.getTotalPages()) {
        this.options.paging.currentPage++;
      }
      return this.remakeContent();
    };

    Grid.prototype.lastPage = function() {
      this.options.paging.currentPage = this.getTotalPages();
      return this.remakeContent();
    };

    Grid.prototype.setPage = function(page) {
      var currentPage, iPage, total;
      total = this.getTotalPages();
      iPage = parseFloat(page);
      if (isNaN(iPage)) {
        iPage = 0;
      }
      currentPage = iPage < 1 ? 1 : iPage > total ? total : iPage;
      this.options.paging.currentPage = currentPage;
      return this.remake();
    };

    Grid.prototype.getTotalPages = function() {
      return Math.ceil(this.data.rows.length / this.options.paging.size);
    };

    Grid.prototype.setPageSize = function(value) {
      this.options.paging.currentPage = 1;
      this.options.paging.size = parseFloat(value);
      return this.remake();
    };

    Grid.prototype.find = function(word) {
      this.data.find(word);
      this.options.paging.currentPage = 1;
      return this.remake();
    };

    Grid.prototype.pageChecked = function() {
      var multiSelect, row, _i, _len, _ref;
      multiSelect = this.getMultiSelect();
      if (multiSelect != null) {
        _ref = this.pageRows();
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          row = _ref[_i];
          if (!row[multiSelect.dataField]) {
            return false;
          }
        }
      }
      return true;
    };

    Grid.prototype.checkPage = function() {
      return this.checkUncheckPage(true);
    };

    Grid.prototype.uncheckPage = function() {
      return this.checkUncheckPage(false);
    };

    Grid.prototype.checkUncheckPage = function(checked) {
      return this.checkUncheck(this.pageRows(), checked);
    };

    Grid.prototype.checkAll = function() {
      return this.checkUncheckAll(true);
    };

    Grid.prototype.uncheckAll = function() {
      return this.checkUncheckAll(false);
    };

    Grid.prototype.checkUncheckAll = function(checked) {
      return this.checkUncheck(this.data.rows, checked);
    };

    Grid.prototype.checkUncheck = function(rows, checked) {
      var multiSelect, row, _i, _len;
      multiSelect = this.getMultiSelect();
      if (multiSelect != null) {
        for (_i = 0, _len = rows.length; _i < _len; _i++) {
          row = rows[_i];
          row[multiSelect.dataField] = checked;
        }
        this.remake();
        return this.updatedMultiSelect();
      }
    };

    Grid.prototype.checkData = function(data) {
      return this.checkUncheckData(data, true);
    };

    Grid.prototype.unhcheckData = function(data) {
      return this.checkUncheckData(data, false);
    };

    Grid.prototype.checkUncheckData = function(data, checked) {
      var equals, multiSelect, obj, prop, properties, row, _i, _j, _k, _len, _len1, _len2, _ref;
      if (!data || data.length === 0) {
        return;
      }
      multiSelect = this.getMultiSelect();
      if (multiSelect != null) {
        properties = $.MEGetPropertyOfObject(data[0]);
        _ref = this.getData();
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          row = _ref[_i];
          for (_j = 0, _len1 = data.length; _j < _len1; _j++) {
            obj = data[_j];
            equals = true;
            for (_k = 0, _len2 = properties.length; _k < _len2; _k++) {
              prop = properties[_k];
              if (row[prop] !== obj[prop]) {
                equals = false;
                break;
              }
            }
            if (equals) {
              row[multiSelect.dataField] = checked;
            }
          }
        }
        this.remake();
        return this.updatedMultiSelect();
      }
    };

    Grid.prototype.invert = function() {
      var multiSelect, row, _i, _len, _ref;
      multiSelect = this.getMultiSelect();
      if (multiSelect != null) {
        _ref = this.data.rows;
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          row = _ref[_i];
          row[multiSelect.dataField] = !row[multiSelect.dataField];
        }
        this.remake();
        return this.updatedMultiSelect();
      }
    };

    Grid.prototype.updatedMultiSelect = function() {
      if (this.check != null) {
        this.check.get(0).checked = this.pageChecked();
      }
      return this.table.triggerHandler("multiSelect.rhd.grid");
    };

    Grid.prototype.getCheckedRows = function() {
      var multiSelect, row, rows, _i, _len, _ref;
      rows = [];
      multiSelect = this.getMultiSelect();
      if (multiSelect != null) {
        _ref = this.getData();
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          row = _ref[_i];
          if (row[multiSelect.dataField]) {
            rows.push(row);
          }
        }
      }
      return rows;
    };

    Grid.prototype.getData = function() {
      return this.data.getData();
    };

    Grid.prototype.getModelData = function(unformatted) {
      var col, data, model, row, _i, _j, _len, _len1, _ref, _ref1;
      data = [];
      _ref = this.getData();
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        row = _ref[_i];
        model = {};
        _ref1 = this.getColumns();
        for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
          col = _ref1[_j];
          model[col.dataField] = unformatted ? col.getModelValue(row) : col.getFormattedValue(row);
        }
        data.push(model);
      }
      return data;
    };

    return Grid;

  })();

  GridData = (function() {

    function GridData(grid, options) {
      this.grid = grid;
      this.onLoadData = __bind(this.onLoadData, this);

      this.url = options.url, this.method = options.method, this.postData = options.postData, this.init = options.init, this.rows = options.rows;
      if (!(this.rows != null)) {
        this.rows = [];
      }
      if (this.init) {
        this.loadData();
      }
    }

    GridData.prototype.loadData = function(url, postData, method) {
      if (this.grid.options.paging) {
        this.grid.options.paging.currentPage = 1;
      }
      this.grid.showDialog($.rhd.grid.locale.loadText);
      return $.ajax({
        type: method || this.method,
        url: url || this.url,
        dataType: 'json',
        data: postData || this.postData,
        success: this.onLoadData
      });
    };

    GridData.prototype.onLoadData = function(data, status, xhr) {
      this.setData(data != null ? data : []);
      this.grid.table.triggerHandler("loadComplete.rhd.grid", data, status, xhr);
      if (this.rows.length === 0) {
        this.grid.showDialog($.rhd.grid.locale.noRecord);
        return this.grid.remakeComponents();
      } else {
        this.prepareData();
        return this.grid.ready();
      }
    };

    GridData.prototype.getData = function() {
      return this.allRows;
    };

    GridData.prototype.setData = function(rows) {
      this.rows = rows || [];
      this.allRows = this.rows;
      this.grid.sort();
      return this.grid.table.triggerHandler("data.rhd.grid");
    };

    GridData.prototype.prepareData = function() {
      var col, row, _i, _len, _ref, _results;
      _ref = this.grid.getColumns();
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        col = _ref[_i];
        if (!(col.multiSelect && (!(col.dataField != null) || col.dataField === ""))) {
          continue;
        }
        col.dataField = "me_grid_sel";
        _results.push((function() {
          var _j, _len1, _ref1, _results1;
          _ref1 = this.allRows;
          _results1 = [];
          for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
            row = _ref1[_j];
            _results1.push(row[col.dataField] = false);
          }
          return _results1;
        }).call(this));
      }
      return _results;
    };

    GridData.prototype.find = function(word) {
      var columns, data, row, _i, _len;
      this.rows = [];
      columns = this.grid.getActiveColumns();
      data = this.allRows;
      for (_i = 0, _len = data.length; _i < _len; _i++) {
        row = data[_i];
        if (this.findHasValue(row, word, columns)) {
          this.rows.push(row);
        }
      }
      return this.grid.table.triggerHandler("filter.rhd.grid");
    };

    GridData.prototype.findHasValue = function(row, word, columns) {
      var col, _i, _len;
      for (_i = 0, _len = columns.length; _i < _len; _i++) {
        col = columns[_i];
        if (col.getFormattedValue(row).toLowerCase().search(word.toLowerCase()) >= 0) {
          return true;
        }
      }
      return false;
    };

    return GridData;

  })();

  GridColumn = (function() {

    function GridColumn(options) {
      var _ref;
      this.options = options;
      _ref = this.options, this.name = _ref.name, this.title = _ref.title, this.dataField = _ref.dataField, this.dataType = _ref.dataType, this.active = _ref.active, this.order = _ref.order, this.sort = _ref.sort, this.multiSelect = _ref.multiSelect;
      this.dataType = this.dataType || "string";
      if (this.active == null) {
        this.active = true;
      }
    }

    GridColumn.prototype.fill = function(str, row) {
      var _this = this;
      if (!(str != null)) {
        return "";
      }
      return str.replace(/\{(.*?)\}/g, function() {
        return _this.getValue(arguments[1], row);
      });
    };

    GridColumn.prototype.getModelValue = function(row) {
      return this.getValue(this.dataField, row);
    };

    GridColumn.prototype.getModelSortValue = function(row) {
      return this.getValue(this.sort.dataField, row);
    };

    GridColumn.prototype.getFormattedValue = function(row) {
      var formatter, options, value;
      formatter = $.rhd.grid.formatter[this.dataType] || $.rhd.grid.formatter.string;
      options = $.extend(true, $.rhd.grid.format[this.dataType], this.options.formatterData);
      if ($.isFunction(this.options.formatter)) {
        formatter = this.options.formatter;
      } else if (this.options.formatter != null) {
        formatter = $.rhd.grid.formatter[this.options.formatter];
        if (!(formatter != null)) {
          formatter = eval(this.options.formatter);
        }
      }
      value = formatter != null ? formatter.call(this, this.getModelValue(row), options) : void 0;
      return value;
    };

    GridColumn.prototype.getValue = function(dataField, value) {
      return $.rhd.grid.functions.getValue(dataField, value);
    };

    return GridColumn;

  })();

  GridTypes = (function() {

    function GridTypes() {}

    GridTypes.sortType = {
      none: "none",
      asc: "asc",
      desc: "desc"
    };

    return GridTypes;

  })();

  GridDefault = (function(_super) {

    __extends(GridDefault, _super);

    function GridDefault() {
      this.makeNavBarItems = __bind(this.makeNavBarItems, this);

      this.makeSearch = __bind(this.makeSearch, this);
      return GridDefault.__super__.constructor.apply(this, arguments);
    }

    GridDefault.prototype.make = function() {
      GridDefault.__super__.make.call(this);
      return this.makeNavBar();
    };

    GridDefault.prototype.remake = function() {
      GridDefault.__super__.remake.call(this);
      return this.navBar.show();
    };

    GridDefault.prototype.refresh = function() {
      GridDefault.__super__.refresh.call(this);
      return this.quickSearch.val('');
    };

    GridDefault.prototype.makeNavBar = function() {
      this.navBar = $("<div>", {
        "class": "simple-navbar"
      });
      this.navBar.append(this.makeSearch());
      this.navBar.append(this.makeNavBarItems());
      this.navBar.prependTo(this.container);
      return this.navBar.hide();
    };

    GridDefault.prototype.makeSearch = function() {
      var _this = this;
      this.quickSearch = $("<input>", {
        type: "text",
        "class": "form-control input-sm",
        placeholder: $.rhd.grid.locale.quickSearch
      });
      this.quickSearch.keypress(function(event) {
        var charCode;
        charCode = event.which != null ? event.which : event.keyCode;
        if (charCode === 13) {
          _this.find(_this.quickSearch.val());
        }
        return true;
      });
      return this.quickSearch;
    };

    GridDefault.prototype.makeNavBarItems = function() {
      var div,
        _this = this;
      div = $("<div>");
      this.summary = $("<span>", {
        "class": "summary"
      });
      this.summary.appendTo(div);
      this.table.on("summary.nav.rhd.grid", function(e, str) {
        return _this.summary.html(str);
      });
      this.buttonsGroup = $("<span/>");
      this.buttonsGroup.appendTo(div);
      this.table.on("buttons.nav.rhd.grid", function(e, btnGroup) {
        return _this.buttonsGroup.html(btnGroup);
      });
      this.optionsGroup = $("<span/>");
      this.optionsGroup.appendTo(div);
      this.table.on("options.nav.rhd.grid", function(e, btnGroup) {
        $(btnGroup).addClass("pull-right options");
        return _this.optionsGroup.html(btnGroup);
      });
      return div;
    };

    return GridDefault;

  })(Grid);

  $.fn.extend({
    jGrid: function(options) {
      var defaults, _arguments, _isMethod, _result;
      defaults = {
        responsive: true
      };
      _isMethod = typeof options === "string";
      _arguments = Array.prototype.slice.call(arguments, 1);
      _result = this;
      this.each(function() {
        var obj, table;
        table = $(this);
        obj = table.data("rhd.grid");
        if ((obj != null) && !_isMethod) {
          return;
        }
        if (_isMethod) {
          return _result = obj[options].apply(obj, _arguments);
        } else {
          options = $.extend(defaults, options);
          return new GridDefault(table, options);
        }
      });
      return _result;
    }
  });

}).call(this);
